// pages/assignment/assignment.js
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
const {
  requestDataparams
} = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    responsiblePersonId:'',
    consignorId: '',
    servechiceindex: '21212',
    customerServiceshow: false,
    selecrindex: '',
    lists: [{
        name: '主管1',
        id: 1
      },
      {
        name: '主管1',
        id: 1
      },
      {
        name: '主管1',
        id: 1
      },
      {
        name: '主管1',
        id: 1
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var userRoles = wx.getStorageSync("userRoles");
    if (userRoles.indexOf('customerService') != -1) {
      this.setData({
        customerServiceshow: true
      })
    }
    if (options.workid) {
      this.data.workid = options.workid;
    }
    if (options.consignorId) {
      this.data.responsiblePersonId = options.consignorId;
    }
    
    this.setData(this.data);
    this.loadAreas();
  },
  loadAreas() {
    var that = this;
    var id = wx.getStorageSync("id");
    let url = '/api/sysUsers?sysUserId=' + id;
    // wx.request({
    //   url: applet_park_api_url + '/api/sysUsers?sysUserId=' + id,
    //   data:{
    //     responsiblePersonId: that.data.responsiblePersonId
    //   },
    //   success(res) {
    //     var lists = [];
    //     if (res.data.code == 0) {
    //       for (let item of res.data.data) {
    //         
    //         item.isSelected = false;
    //         lists.push(item);
    //       }
    //       that.setData({
    //         lists: lists
    //       })

    //       
    //     } else {
    //       wx.showToast({
    //         title: '网络异常',
    //       })
    //     }
    //   },
    //   fail(res) {
    //     wx.showToast({
    //       title: res.data,
    //     })
    //   }
    // })

    requestDataparams(url, {}, 'GET').then(res => {
      var lists = [];
      if (res.data.code == 0) {
        for (let item of res.data.data) {
          
          item.isSelected = false;
          lists.push(item);
        }
        that.setData({
          lists: lists
        })

        
      } else {
        wx.showToast({
          title: '网络异常',
        })
      }
    }).catch(err => {
      
    });
  },
  // selectTAg(){
  //   // inx = e.selectTAg/
  // },
  // 选择
  selectTAg: function(e) {
    var index = e.currentTarget.dataset.index;
    var item = this.data.lists[index]
    item.isSelected = !item.isSelected;

    var chooseList = '';
    var n = 0;
    for (var item of this.data.lists) {
      if (item.isSelected == true) {
        chooseList = chooseList + item.id + ",";
        n++;
      }
    }
    chooseList = chooseList.substr(0, chooseList.length - 1);
    
    // this.data.queryParams['vehicleType'] = chooseList;
    // this.data.queryParams['pageNo'] = 0;
    // this.data.showserch = false;
    // this.data.items = [];
    // this.data.totalCount = 0;
    this.data.otherConsignor = chooseList;
    this.setData(this.data);
    // this.loadData();
  },
  serverselectTAg(e) {
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var pages = getCurrentPages();

    var prevPage = pages[pages.length - 2]; //上一个页面

    prevPage.setData({
      consignorId: id,
      consignorNmae: name
    });
    wx.navigateBack();
  },
  submit(){
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    let otherConsignor = this.data.otherConsignor
    prevPage.setData({
      otherConsignor: otherConsignor,
    
    });
    wx.navigateBack();
  },
  servesubmit() {
    var that = this;
    var id = wx.getStorageSync("id");
    wx.request({
      url: applet_park_api_url + '/api/repairs/customerAppoint',
      method: "PUT",
      data: {
        id: that.data.workid,
        assignorId: id,
        consignorId: that.data.consignorId
      },
      success(res) {
        if (res.data.code == 0) {

          var pages = getCurrentPages();

          var prevPage = pages[pages.length - 2]; //上一个页面
          prevPage.loadData();
          wx.navigateBack();
          
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  gotoainsatt() {
    wx.navigateTo({
      url: '',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})